<?php
/**
 * 此模型不包含任何数据库操作 这里为后端
 * 赋值和视图处理
 * 对于扩展字段来说 这里只有1 新闻扩展字段
 * @author Wolf
 *
 */
class FactoryController extends NodeController
{
    /**
     * 检查标题是否重复
     */
    public function checktitle ()
    {
        if ($_POST['title']) {
            $title = trim($_POST['title']);
            $rs = NewsModel::instance()->getNewsByWhere(
            array('title' => $title), null, 1);
            if (! empty($rs)) {
                // 准确搜索
                $error = '[' . $rs[0]['category_name'] .
                 ']<a href="/news/v/?id=' . $rs[0]['id'] . '" target="__blank" >' .
                 $rs[0]['title'] . "</a>";
                $this->sendNotice($error, $rs);
            }
            $rs = NewsModel::instance()->getOneNewsLikeTitle($title, 3);
            if (! empty($rs)) {
                foreach ($rs as $v) {
                    $data .= $v['title'] . "|";
                }
                $this->sendNotice($data, $rs, false);
            } else {
                $this->sendNotice("OK", null, true);
            }
        }
    }
    /**
     * 清除所有缓存 增加编译缓存 高性能
     */
    public function cleanCache ()
    {
        //清楚统计数据缓存
        $num = self::getNewsService()->truncate();
        //清除smarty 缓存
        $t = $this->view()->clearAllCache();
        $c = $this->view()->clearCompiledTemplate();
        $notice = 'Clean' . $t . ' html file ' . $c . ' compiled file';
        $this->sendNotice($notice);
    }
    /**
     * 查看配置信息
     */
    public function config ()
    {
        $config = require_once 'base.php';
        $s = require_once 'smarty.php';
        $this->view()->assign('cachetype', 1);
        $this->view()->assign('config', $config);
        $this->view()->assign('smartyconfig', $s);
        $this->view()->assign("get_magic_quotes_gpc", get_magic_quotes_gpc());
        $ini = ini_get_all();
        $this->view()->assign('zone', $ini['date.timezone']['local_value']);
        $this->view()->assign('sessionpath', 
        $ini['session.save_path']['local_value']);
        $this->view()->assign('sessionstart', 
        $ini['session.auto_start']['local_value']);
        $this->view()->assign('pdosocket', 
        $ini['pdo_mysql.default_socket']['local_value']);
        $this->view()->assign('ini', $ini);
        $system = array('sys' => php_uname(), 'phpapi' => php_sapi_name(), 
        'phpversion' => PHP_VERSION, 'phppath' => DEFAULT_INCLUDE_PATH, 
        'httphost' => $_SERVER['HTTP_HOST'], 
        'cpus' => $_SERVER['PROCESSOR_IDENTIFIER']);
        $this->view()->assign('system', $system);
        $this->view()->display("file:news/config.tpl");
    }
    /**
     * 排序
     */
    public function s ()
    {
        if (isset($_POST['sort']) && $_POST['sort'] >= 0) {
            $sort = trim($_POST['sort']);
            self::getNewsService()->setNewsSort($sort, $_POST['id']);
            echo 'SUCCESS!';
            // 清除缓存
            exit();
        } else {
            echo 'Failed!';
            exit();
        }
    }
    public function c ()
    {
        //  $this->validteNode(ADMIN); // 加入权限验证
        //这里穿进去的是$_GET 参数
        //执行代码
        $cid = $_GET['cid'];
        $p = isset($_GET['p']) ? $_GET['p'] : 1;
        $sort = isset($_GET['sort']) ? $_GET['sort'] : 'id';
        //停止xhprof  
        $rs = self::getNewsService()->newsModule->listing($cid, $p, 
        $sort, 40);
        $cate = self::getNewsService()->getCategory($cid, 
        array('module' => $_GET['mid'])); // 调用父id
        $this->view()->assign('category', $cate);
        $this->view()->assign('currentcid', $cid); //当前cid
        $this->view()->assign('news', $rs['newslist']);
        $this->view()->assign('totalnum', $rs['totalnum']);
        $this->view()->assign('num', $rs['page']);
        $this->view()->assign('module', $rs['module']);
        //$this->view ()->clearCache ( 'file:/news/list.tpl' );
        $this->view()->display('file:news/list.tpl');
    }
    /**
     * 搜索页面
     */
    public function search ()
    {
        /* 增加扩展字段 也就是分类搜索 */
        if (! isset($_REQUEST['key']) || ! isset($_REQUEST['value']))
            return;
        $newslist = self::getNewsService()->search($_REQUEST['key'], 
        $_REQUEST['value']);
        $this->view()->assign('news', $newslist);
        $this->view()->display('file:news/list.tpl');
    }
    /*添加内容*/
    public function data ()
    {
        $categoryid = isset($_GET['cid']) ? $_GET['cid'] : 1;
        $mid = $_GET['mid'];
        //导入扩展字段
        /**
         * 增加flag标记
         */
        $flag = self::getNewsService()->newsModule->_base->getFlag(); //返回所有标签
        $this->view()->assign('flag', $flag); //导入标签
        $extend = self::getNewsService()->newsModule->_extend->getExtendByCid(
        $categoryid);
        if (! empty($_SESSION['width'])) {
            $this->view()->assign('height', $_SESSION['height']);
            $this->view()->assign('width', $_SESSION['width']);
        }
        //
        $category = self::getNewsService()->getCategory($categoryid, 
        array('module' => $mid));
        $this->view()->assign('category', $category);
        $this->view()->assign("cachetype", 0);
        $this->view()->assign('group', $this->_group);
        $this->view()->assign('module', $mid);
        $this->view()->assign('extend', $extend);
        $temp = self::getNewsService()->newsModule->_m->temp("add");
        $this->view()->display($temp);
    }
    /**
     * 批量复制和编辑新闻
     * 
     */
    public function edit ()
    {
        if (! isset($_POST) || empty($_POST['ids'])) {
            echo "you shoule have select one more cateogry or news!";
            exit();
        }
        self::getNewsService()->edit($_POST['ids'], $_POST['cid'], 
        $_POST['type']);
        // 清除缓存
        $this->sendNotice("success", null, true);
    }
    //模型添加信息
    public function add ()
    {
        if (empty($_POST))
            return;
        $data = $_POST;
        //选择容器
        $lastInsertId = self::getNewsService()->newsModule->_base->add(
        $data);
        //实例化内容模型
        $data['nid'] = $lastInsertId;
        self::getNewsService()->newsModule->_extend->add($data);
        self::getNewsService()->newsModule->_m->add($data);
        //生成静态文件 暂时没有考虑权限问题
        $this->vHtml($lastInsertId);
        $this->redirect(SUCCESS, 
        "/factory/data/?cid=$_POST[cid]&mid=$_POST[mid]");
         //结果 返回结果
    }
    /**
     * 根据内容的分类 绑定所需模板
     */
    public function vHtml ($id)
    {
        //如果链接自带模板 覆盖上方          
        $news = self::getNewsService()->setConTemp($id, null);
        // 设置绑定模板
        if (! $news) {
            return;
        }
        $this->view()->assign('content', $news['content']);
        $content = $this->view()->fetch($news['tempname']);
        self::getNewsService()->html(null, $id, $content, "V", 
        $news['content']['date']);
    }
    /**
     * 
     * 编辑页面
     * */
    public function v ()
    {
        $nid = $_GET['id'];
        $mid = $_GET['mid'];
        if (isset($_GET['preid'])) {
            $this->view()->assign('preid', $_GET['preid']);
        }
        if (isset($_GET['p'])) {
            $this->view()->assign('p', $_GET['p']);
        }
        if (! empty($_SESSION['width'])) {
            $this->view()->assign('height', $_SESSION['height']);
            $this->view()->assign('width', $_SESSION['width']);
        }
        $news = self::getNewsService()->newsModule->getCon($nid); //一键获取内容
        //结合自身的
        $flag = self::getNewsService()->newsModule->_base->getFlag();
        $this->view()->assign('flag', $flag);
        $category = self::getNewsService()->getCategory(
        $news['content']['cid'], array('module' => $news['content']['mid']));
        $this->view()->assign('category', $category);
        $this->view()->assign('group', $this->_group);
        // 为了兼容之间新闻 可能没有进行过滤 2013-08-20
        $this->view()->assign('extend', $news['extend']);
        $this->view()->assign('news', $news['content']);
        $this->view()->assign('module', $this->_mid);
        $temp = self::getNewsService()->newsModule->temp("edit");
        //$this->view ()->clearCache ( $temp );
        $this->view()->display($temp);
    }
    //模型保存数据
    public function save ()
    {
        $data = $_POST; //获取数据
        $data['nid'] = $data['id']; //转换
        //处理标签
        $preid = isset($_POST['preid']) ? $_POST['preid'] : 1;
        $p = isset($_POST['p']) ? $_POST['p'] : 1;
        $id = $data['id'];
        //一键保存
        $mid = self::getNewsService()->newsModule->save($data, $id);
        //生成静态文件
        $this->vHtml($id);
        $this->redirect(SUCCESS, "/factory/v/?id=$id&mid=$mid", 2);
         //结果 返回结果
    }
    /**
     * 删除内容
     * Enter description here ...
     */
    public function delCon ()
    {
        if (! isset($_REQUEST['id']) || $_REQUEST['id'] < 0)
            return;
        self::getNewsService()->newsModule->_m->remove(
        array('id' => $_REQUEST['id']));
        $this->sendNotice(SUCCESS);
    }
    /**
     * 清空 包括图片
     * Enter description here ...
     */
    public function clean ()
    {
        //不需要传递参数 直接将回收站的东西清空
        //删除缩略图
        self::getNewsService()->batchArticle();
         //删除文章图片
    }
    /**
     * 
     * 获取newsService
     */
    public static function getNewsService ()
    {
        return new NewsService($_REQUEST['cid'], $_REQUEST['mid']);
    }
}